<template>
  <div class="order-detail">
    <LayoutHeaderTitle :title="orderInfo.order_status" />
    <template v-if="orderInfo.list">
      <div
        class="list-item wrapper"
        v-for="item in orderInfo.list"
        :key="item.b_id"
      >
        <div class="head">
          <nuxt-link :to="item.b_id | storeUrl">
            {{ item.b_name }} <van-icon name="arrow" />
          </nuxt-link>
        </div>
        <div class="goods" v-for="g in item.goods" :key="g.order_detail_id">
          <div class="info cl">
            <img class="img" :src="g.g_src | formatSrc" :alt="g.g_name" />
            <div class="content">
              <van-row align="bottom" type="flex" justify="space-between">
                <p class="name text-over">{{ g.g_name }}</p>
                <p class="name text-over price">
                  <small>￥{{ g.price }}</small>
                </p>
              </van-row>
              <van-row align="bottom" type="flex" justify="space-between">
                <p class="spec text-over2">{{ g.spec_name }}</p>
                <p class="spec">
                  <small>x{{ g.num }}</small>
                </p>
              </van-row>
            </div>
          </div>
          <div class="btn">
            <nuxt-link :to="{ name: 'goods-id', params: { id: g.g_id } }">
              <van-button plain size="small" round>
                再次购买
              </van-button>
            </nuxt-link>
          </div>
        </div>
      </div>
    </template>
    <div class="order-price wrapper">
      实付款:<span>￥{{ orderInfo.order_price }}</span>
    </div>
    <div class="order-info wrapper">
      <div class="title">订单信息</div>
      <div class="row">
        <div class="label">订单编号:</div>
        <div class="content">{{ orderInfo.order_id }}</div>
      </div>
      <div class="row">
        <div class="label">收货信息:</div>
        <div class="content">{{ getAddress }}</div>
      </div>
      <div class="row">
        <div class="label">创建时间:</div>
        <div class="content">{{ orderInfo.create_time }}</div>
      </div>
      <div class="row" v-if="orderInfo.delivery_time">
        <div class="label">发货时间:</div>
        <div class="content">{{ orderInfo.delivery_time }}</div>
      </div>
      <div class="row" v-if="orderInfo.receipt_time">
        <div class="label">收货时间:</div>
        <div class="content">{{ orderInfo.receipt_time }}</div>
      </div>
      <div class="row" v-if="orderInfo.finish_time">
        <div class="label">完成时间:</div>
        <div class="content">{{ orderInfo.finish_time }}</div>
      </div>
    </div>
    <div class="fixed-bottom" v-if="orderInfo.order_status !== '已退款'">
      <template v-if="orderInfo.order_status === '已支付'">
        <van-button type="default" size="small" round>
          等待发货~
        </van-button>
        <van-button
          type="danger"
          plain
          round
          size="small"
          @click="
            setModal(
              'refund',
              '申请退款',
              `申请退款后需要<span class='red'>同意</span>后才能完成退款，是否继续`
            )
          "
        >
          申请退款
        </van-button>
      </template>
      <template v-else-if="orderInfo.order_status === '已发货'">
        <van-button type="default" size="small" round>
          查看物流
        </van-button>
        <van-button
          color="#ff6034"
          plain
          round
          size="small"
          @click="
            setModal(
              'sign',
              '确定收货',
              `<span class='red'>此操作不可逆</span>，请确定收到货物之后再来确认收货`
            )
          "
        >
          确认收货
        </van-button>
      </template>
      <template v-else-if="orderInfo.order_status === '已收货'">
        <van-button
          type="info"
          plain
          size="small"
          round
          @click="
            $router.push({
              name: 'order-comment',
              params: { id: item.order_id }
            })
          "
        >
          去评价
        </van-button>
      </template>
      <template v-else-if="orderInfo.order_status === '待退款'">
        <van-button
          type="primary"
          plain
          round
          size="small"
          @click="
            setModal(
              'norefund',
              '取消退款',
              `<span class='red'>当前订单正在等待商家同意退款</span>，取消后不再退款，是否继续`
            )
          "
        >
          取消退款
        </van-button>
      </template>
      <template v-else>
        <van-button plain round size="small">
          查看评价
        </van-button>
      </template>
    </div>
    <ModalConfirm
      :title="modalTitle"
      :desc="modalDesc"
      v-model="isShowModal"
      @submit="checkOrder"
      @close="isShowModal = false"
    />
  </div>
</template>

<script>
export default {
  async asyncData({ app, params }) {
    const { id } = params;
    let orderInfo = {};
    try {
      const { data } = await app.$apiGetOrderInfo({ order_id: id });
      orderInfo = data;
    } catch (error) {}
    return {
      orderInfo
    };
  },
  layout: "notabbar",
  data: () => ({
    isShowModal: false,
    modalDesc: "",
    modalTitle: "",
    type: ""
  }),
  methods: {
    // 弹窗确定
    checkOrder() {
      const data = {
        type: this.type,
        order_id: this.orderInfo.order_id
      };
      this.$apiUpdateOrder(data).then(res => {
        if (res.status === 0) {
          this.$toast(res.msg);
          setTimeout(() => {
            this.$router.go(-1);
          }, 1000);
        }
      });
    },
    // 设置弹窗
    setModal(type, title, desc) {
      this.type = type;
      this.modalTitle = title;
      this.modalDesc = desc;
      this.isShowModal = true;
    }
  },
  computed: {
    getAddress() {
      if (!this.orderInfo.address) {
        return "";
      }
      const { name, address, tel } = this.orderInfo;
      return `${name} ,${tel} ,${address}`;
    }
  }
};
</script>

<style lang="scss" scoped>
.order-detail {
  background: #efefef;
  padding: 50px 0 50px;
  font-size: 16px;
  height: 100vh;
  overflow-y: auto;
  box-sizing: border-box;
  .wrapper {
    padding: 10px;
    background: #fff;
    margin-top: 10px;
  }
  .head {
    display: flex;
    font-weight: bold;
    margin-bottom: 5px;
    justify-content: space-between;
    a {
      display: flex;
      align-items: center;
      color: #000;
    }
    span {
      font-weight: normal;
      font-size: 14px;
      color: red;
    }
  }
  .list-item {
    color: #000;
    .info {
      margin-top: 10px;
      display: flex;
      align-items: flex-start;
      color: #000;
      img {
        width: 120px;
        height: 120px;
        margin-right: 10px;
        border-radius: 8px;
        float: left;
      }
      .name {
        font-weight: 400;
        line-height: 20px;
        width: 180px;
        &.price {
          width: auto;
        }
      }
      p {
        margin: 0;
      }
      .spec {
        margin-top: 10px;
        padding: 2px 5px;
        max-width: 160px;
        font-size: 12px;
        color: #cbcaca;
      }
    }
    .btn {
      margin: 10px 0;
      display: flex;
      justify-content: flex-end;
      ::v-deep .van-button {
        width: 85px;
        height: 26px;
        margin-left: 15px;
      }
    }
  }
  .order-price {
    text-align: right;
    font-size: 14px;
    font-weight: bold;
    span {
      color: #ff976a;
    }
  }
  .order-info {
    .title {
      font-size: 16px;
      margin-bottom: 10px;
    }
    .row {
      padding: 10px 0;
      font-size: 12px;
      display: flex;
      align-items: center;
      .label {
        width: 100px;
        flex-shrink: 0;
      }
      .content {
        word-break: break-word;
      }
    }
  }
}
.fixed-bottom {
  position: fixed;
  height: 50px;
  bottom: 0;
  right: 0;
  left: 0;
  padding: 0 15px;
  box-sizing: border-box;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  background: #fff;
  font-size: 12px;
  ::v-deep .van-button {
    width: 1.7rem;
    height: 0.52rem;
    margin-left: 0.3rem;
  }
}
</style>
